package PartEdgeDemo;

import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;

import y.geom.LineSegment;
import y.geom.YPoint;

/**
 * Created by IntelliJ IDEA.
 * User: zielke
 * Date: 9/27/11
 * Time: 9:52 AM
 * To change this template use File | Settings | File Templates.
 */
public class PathOperation {

  static double epsilon = 10e-5;

  final static YPoint getStartOfPath(GeneralPath path, boolean source){
    double[] coords = new double[6];
    double[] oldcoords = new double[6];

    PathIterator pathIterator = path.getPathIterator(new AffineTransform());
    if(source){
      while(!pathIterator.isDone() && pathIterator.currentSegment(coords) == PathIterator.SEG_MOVETO){
        pathIterator.next();
        System.arraycopy(coords,0,oldcoords,0,6);
      }
      return new YPoint(oldcoords[0], oldcoords[1]);
    }else{
      PathIterator reversePathIterator = new ReversePathIterator(pathIterator);
      while(!reversePathIterator.isDone() &&  reversePathIterator.currentSegment(coords) == PathIterator.SEG_MOVETO){
        reversePathIterator.next();
        System.arraycopy(coords,0,oldcoords,0,6);
      }
      
      
      return new YPoint(oldcoords[0], oldcoords[1]);
    }
  }

  final static boolean intersectsWithParts(LineSegment edge, YPoint intersection, int value){
    //intersection is a YPoint that lies on the edge
    double edgeLength = edge.length();
    double distanceToIntersection = new LineSegment(edge.getFirstEndPoint(), intersection).length();
    double relation = distanceToIntersection / edgeLength * 100;
    return ((relation-epsilon < value) || (relation+epsilon > 100-value));
  }
  
  
  final static double overlappingPercent(LineSegment edge, YPoint intersection, int value){
	  double edgeLength = edge.length();
      double wa = value + 0.0; // sonst wäre percentz = 0
      double percentz = (wa/100);
      double oneSidePart = edgeLength * percentz;
      
	  return  overlappingStub(edge, intersection, value) / oneSidePart * 100;
  }
  
  
  final static double overlappingStub(LineSegment edge, YPoint intersection, int value){
	  double edgeLength = edge.length();
	  double distanceToIntersection = 0;
	  
	  double firstEndDistanceToIntersection1 = edge.getFirstEndPoint().distanceTo(intersection);
      double secondEndDistanceToIntersection1 = edge.getSecondEndPoint().distanceTo(intersection);	
      
      double wa = value + 0.0; // sonst wäre percentz = 0
      double percentz = (wa/100);
      double oneSidePart = edgeLength * percentz;
      double middlePart = edgeLength - 2 * oneSidePart;
      
      double stubLength = 0;
          
        if(firstEndDistanceToIntersection1 < secondEndDistanceToIntersection1){
        	distanceToIntersection = firstEndDistanceToIntersection1;
        }else{      	
        	distanceToIntersection = secondEndDistanceToIntersection1;
        }
        stubLength =  edgeLength - middlePart - oneSidePart - distanceToIntersection;
    	return stubLength;    

  }
  
  

  
  
  
  
}
